package geppetto.main;


import geppetto.cat.alignments.Alignment;
import geppetto.cat.alignments.AlignmentsSet;
import geppetto.cat.alignments.output.AlignerOutputLatex;
import geppetto.cat.corpus.BilingualCorpus;

import java.io.IOException;
import java.io.PrintStream;



public class PrettyPrintAlignmentsWithPosteriors {

    public static AlignmentsSet readAlignments(BilingualCorpus corpus, String alignmentFile, byte sentenceSource, String alignmentFormat) throws IOException {
	AlignmentsSet set = null;
	if (alignmentFormat.equals("moses")) {
	    set = AlignmentsSet.getAlignmentFromMoses(alignmentFile, sentenceSource, corpus);
	} else if (alignmentFormat.equals("posterior")) {
	    set = AlignmentsSet.getAlignmentFromPosterior(alignmentFile, sentenceSource, corpus);
	} else {
	    System.out.println("Read method not implemented" + alignmentFormat);
	    throw new UnsupportedOperationException();
	}
	return set;
    }

    public static void main(String[] args) throws Exception {
	String corpusDescription = args[0];
	int size = Integer.parseInt(args[1]);
	int maxSentenceSize = Integer.parseInt(args[2]);
	String outputDir = args[3];
	String alignmentFile = args[4];
	String alignmentSourceS = args[5];
	String alignmentFormat = args[6];
	String alignmentNumber = null;
	if(args.length >= 8){
		alignmentNumber = args[7];
	}

	System.out.println("Corpus: " + corpusDescription);
	System.out.println("Size: " + size);
	System.out.println("Max Sentence size: " + maxSentenceSize);
	System.out.println("outputDir: " + outputDir);
	System.out.println("Load bilingual corpus");
	System.out.println("Alignments to print: " + alignmentNumber);

	BilingualCorpus corpus = BilingualCorpus.getCorpusFromFileDescription(corpusDescription, size, maxSentenceSize);

	byte alignmentSource;
	if (alignmentSourceS.equals("train")) {
	    alignmentSource = BilingualCorpus.TRAIN_CORPUS;
	} else if (alignmentSourceS.equals("test")) {
	    alignmentSource = BilingualCorpus.TEST_CORPUS;
	} else if (alignmentSourceS.equals("devel")) {
	    alignmentSource = BilingualCorpus.DEV_CORPUS;
	} else {
	    System.out.println("Invalid sentence source");
	    throw new UnsupportedOperationException();
	}

	System.out.println("Reading Alignment file: " + alignmentFile);
	System.out.println("Reading Alignment from source: " + alignmentSourceS);
	System.out.println("Reading Alignment in format : " + alignmentFormat);

	AlignmentsSet alignments = readAlignments(corpus, alignmentFile, alignmentSource, alignmentFormat);
	PrintStream printer = new PrintStream(outputDir);
	AlignerOutputLatex.printBegin(printer);
	AlignmentsSet gold;

	if (corpus.getGold() != null) {
	    gold = corpus.getGold();
	} else {
	    gold = new AlignmentsSet();
	    for (Alignment al : alignments.getAlignments()) {
		Alignment newAl = new Alignment(al);
		newAl.resetPoints();
		newAl.resetPosteriors();
		gold.addAlignment(newAl);
	    }
	}

	if(alignmentNumber != null){
		AlignerOutputLatex.outputWithGoldPostLatex(alignments, gold, corpus, printer, maxSentenceSize, "", alignmentNumber);
	}
	else{
		AlignerOutputLatex.outputWithGoldPostLatex(alignments, gold, corpus, printer, maxSentenceSize, "");
	}
	AlignerOutputLatex.printEnd(printer);
    }
}
